package com.glory.brepda.activity.mfg;


import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.brepda.R;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.callback.CustomDialogEntityCallBack;
import com.glory.brepda.entity.DefectCodeEntity;
import com.glory.brepda.entity.ToolEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.entity.waferAbortEntity;
import com.glory.brepda.entity.wbScheduleEntity;
import com.glory.brepda.utils.NetUtils;
import com.glory.brepda.utils.ShowDialog;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_CommonCallback;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class WaferAbortActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.etOtherReason)
    FreeUI_ClearEditText etOtherReason;
    @BindView(R.id.llOtherReason)
    LinearLayout llOtherReason;
    @BindView(R.id.waferLotId)
    FreeUI_ClearEditText waferLotId;
    @BindView(R.id.customerCode)
    TextView customerCode;
    @BindView(R.id.mainMatType)
    TextView mainMatType;
    @BindView(R.id.productionDesc)
    TextView productionDesc;
    @BindView(R.id.waferDesc)
    TextView waferDesc;
    @BindView(R.id.woId)
    TextView woId;
    @BindView(R.id.sourceId)
    TextView sourceId;
    @BindView(R.id.flReasonList)
    FrameLayout flReasonList;
    @BindView(R.id.tvReason)
    TextView tvReason;
    @BindView(R.id.ivReasonArrow)
    ImageView ivReasonArrow;

    //用于进行处理的组件
    @BindView(R.id.customerCode2)
    TextView customerCode2;
    @BindView(R.id.mainMatType2)
    TextView mainMatType2;
    @BindView(R.id.productionDesc2)
    TextView productionDesc2;
    @BindView(R.id.waferDesc2)
    TextView waferDesc2;
    @BindView(R.id.woId2)
    TextView woId2;
    @BindView(R.id.sourceId2)
    TextView sourceId2;
    @BindView(R.id.abortReason)
    TextView abortReason;
    @BindView(R.id.etWaferLoss)
    FreeUI_ClearEditText etWaferLoss;

    private List<String> mReasonList =new ArrayList<>();
    private FreeUI_GeneralPop mReasonPop;
    private String requestFlag;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_wafer_abort;
    }

    @Override
    protected void initView() {
        setMainLayout(llMain);
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        tvBaseSubmit.setText("提交");

    }



    @Override
    protected void initData() {
        llOtherReason.setVisibility(View.GONE);
        initEdit(waferLotId, this::getDetail);
        requestReasonList();
    }

    @OnClick({R.id.tvBaseSubmit,R.id.flReasonList})
    public void onViewClicked(View view) {
        if(view.getId() == R.id.tvBaseSubmit){
            if (getText(waferLotId).equals("")) {
                showToast("芯片批号不能为空", false);
                return;
            }
            if(tvReason.getText().toString()=="其他" && getText(etOtherReason).equals("") && requestFlag == "abort"){
                showToast("请输入退划片原因",false);
                return;
            }
            if(tvReason.getText().toString().equals("") && requestFlag == "abort"){
                showToast("请选择退划片原因",false);
                return;
            }
            if(getText(etWaferLoss).equals("") && requestFlag == "handle"){
                showToast("芯片损失数不能为空", false);
                return;
            }
            if (requestFlag == "abort"){
                FreeApi_DialogUtils.getConfirmAndCancelDialog(this, "确定要提交?", new FreeUI_CommonCallback() {
                    @Override
                    public void onSuccess() {
                        FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<JSONObject>>
                                (new TypeToken<BaseResponseBean<JSONObject>>() {
                                }.getType(), getSupportFragmentManager(), this) {

                            @Override
                            public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                                super.onSuccess(response);
                                if (response.body().isSuccess(getApplicationContext(),false,getSupportFragmentManager())){
                                    showToast("提交成功",true);
                                }
                            }

                            @Override
                            public void onError(Response<BaseResponseBean<JSONObject>> response) {
                                super.onError(response);
                                loadError(response.getException(),"SUBMIT_DIALOG");
                            }
                        };
                        JsonObject map = new JsonObject();
                        map.addProperty("ACTIONTYPE","SUBMITABORT");
                        map.addProperty("WAFERLOTID",getText(waferLotId));
                        map.addProperty("CUSTOMER_CODE",customerCode.getText().toString());
                        map.addProperty("MAIN_MAT_TYPE",mainMatType.getText().toString());
                        map.addProperty("PRODUCTION_DESC",productionDesc.getText().toString());
                        map.addProperty("WAFER_DESC",waferDesc.getText().toString());
                        map.addProperty("WO_ID",woId.getText().toString());
                        map.addProperty("SOURCE_ID",sourceId.getText().toString());
                        String temp = tvReason.getText().toString();
                        if(tvReason.getText().toString().equals("其他")){
                            map.addProperty("REASON",getText(etOtherReason));
                        }else{
                            map.addProperty("REASON",tvReason.getText().toString());
                        }
                        NetUtils.requestPostNet("IF.WAFERABORT","Common",WaferAbortActivity.this,map,callBack);
                    }
                }, () -> {
                }).show(getSupportFragmentManager(),"SUBMIT_DIALOG");
            }else if(requestFlag == "handle"){
                FreeApi_DialogUtils.getConfirmAndCancelDialog(this, "确定要提交?", new FreeUI_CommonCallback() {
                    @Override
                    public void onSuccess() {
                        FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<JSONObject>>
                                (new TypeToken<BaseResponseBean<JSONObject>>() {
                                }.getType(), getSupportFragmentManager(), this) {

                            @Override
                            public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                                super.onSuccess(response);
                                if (response.body().isSuccess(getApplicationContext(),false,getSupportFragmentManager())){
                                    showToast("提交成功",true);
                                }
                            }

                            @Override
                            public void onError(Response<BaseResponseBean<JSONObject>> response) {
                                super.onError(response);
                                loadError(response.getException(),"SUBMIT_DIALOG");
                            }
                        };
                        JsonObject map = new JsonObject();
                        map.addProperty("ACTIONTYPE","SUBMITHANDLE");
                        map.addProperty("WAFERLOTID",getText(waferLotId));
                        map.addProperty("WAFERLOSS",getText(etWaferLoss));
                        NetUtils.requestPostNet("IF.WAFERABORT","Common",WaferAbortActivity.this,map,callBack);
                    }
                }, () -> {
                }).show(getSupportFragmentManager(),"SUBMIT_DIALOG");
            }
        }else if(view.getId()==R.id.flReasonList) {
            if (mReasonPop != null) {
                if (mReasonPop.isShowing()) {
                    mReasonPop.dismiss();
                }
            } else {
                mReasonPop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flReasonList.getWidth(), mReasonList, ivReasonArrow, (codeS, i) -> {
                    tvReason.setText(codeS);
                    if (tvReason.getText().toString().equals("其他")) {
                        llOtherReason.setVisibility(View.VISIBLE);
                    } else {
                        llOtherReason.setVisibility(View.GONE);
                    }
                });
            }
            FreeApi_PopUtils.setArrowShow(ivReasonArrow);
            mReasonPop.showAsDropDown(flReasonList, 0, 0);
        }
    }

    private void getDetail(String waferLotId) {
        FreeUI_EntityCallBack<BaseResponseBean<waferAbortEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<waferAbortEntity>>
                (new TypeToken<BaseResponseBean<waferAbortEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<waferAbortEntity>> response) {
                super.onSuccess(response);
                String reason = response.body().getResponse().getBody().getReason();
                if (reason == null){
                    requestFlag = "abort";
                    findViewById(R.id.abort).setVisibility(View.VISIBLE);
                    findViewById(R.id.handle).setVisibility(View.GONE);
                    customerCode.setText(response.body().getResponse().getBody().getCustomerCode());
                    mainMatType.setText(response.body().getResponse().getBody().getMainMatType());
                    productionDesc.setText(response.body().getResponse().getBody().getProductionDesc());
                    waferDesc.setText(response.body().getResponse().getBody().getWaferDesc());
                    woId.setText(response.body().getResponse().getBody().getWoId());
                    sourceId.setText(response.body().getResponse().getBody().getSourceId());
                }else{
                    requestFlag = "handle";
                    findViewById(R.id.handle).setVisibility(View.VISIBLE);
                    findViewById(R.id.abort).setVisibility(View.GONE);
                    customerCode2.setText(response.body().getResponse().getBody().getCustomerCode());
                    mainMatType2.setText(response.body().getResponse().getBody().getMainMatType());
                    productionDesc2.setText(response.body().getResponse().getBody().getProductionDesc());
                    waferDesc2.setText(response.body().getResponse().getBody().getWaferDesc());
                    woId2.setText(response.body().getResponse().getBody().getWoId());
                    sourceId2.setText(response.body().getResponse().getBody().getSourceId());
                    abortReason.setText(response.body().getResponse().getBody().getReason());
                }

            }

            @Override
            public void onError(Response<BaseResponseBean<waferAbortEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "waferAbort");
            }
        };

        JsonObject map = new JsonObject();
        map.addProperty("WAFERLOTID",waferLotId);
        map.addProperty("ACTIONTYPE","GETDETAIL");
        NetUtils.requestPostNet("IF.WAFERABORT","Common", WaferAbortActivity.this,map,callBack);
    }


    private void requestReasonList() {
        FreeUI_EntityCallBack<BaseResponseBean<DefectCodeEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<DefectCodeEntity>>
                (new TypeToken<BaseResponseBean<DefectCodeEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<DefectCodeEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(),true,getSupportFragmentManager())){
                    List<DefectCodeEntity.DefectCode> scrapcodelists = response.body().getResponse().getBody().getADREFLIST();
                    for (DefectCodeEntity.DefectCode code : scrapcodelists) {
                        mReasonList.add(code.getKEY());
                    }
                }

            }

            @Override
            public void onError(Response<BaseResponseBean<DefectCodeEntity>> response) {
                super.onError(response);
                loadError(response.getException(),"ADREFLISTQUERY");
            }
        };

        JsonObject map = new JsonObject();
        map.addProperty("REFERENCETYPE","USER");
        map.addProperty("REFERENCENAME","WaferAbortReason");

        NetUtils.requestPostNet("IF.ADREFLISTQUERY","Common", WaferAbortActivity.this,map,callBack);
    }


}
